package edu.infomatik.tundra.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;

import edu.infomatik.tundra.data.DAOtundra;
import edu.infomatik.tundra.data.IBean;
import edu.infomatik.tundra.data.MockWorkBean;
import edu.infomatik.tundra.logic.SprintBean;
import edu.infomatik.tundra.logic.USTaskBean;
import edu.infomatik.tundra.logic.UStoryBean;
import edu.infomatik.tundra.logic.WorkBean;
import edu.infomatik.tundra.logic.WorkKinds;
import edu.infomatik.tundra.logic.security.SystemAccessBean;

@SuppressWarnings("unused")
public class WorkList {
	@SessionState
	private SystemAccessBean user;
	private boolean userExists;
	private List<WorkBean> wbList;
	@SessionState
	@Property
	private WorkBean work;
	@SessionState
	private SprintBean sprint;
	@SessionState
	private UStoryBean story;
	@SessionState
	private USTaskBean task;
	@Inject
	private Messages message;
	@SessionState
	private Boolean newWork=new Boolean(false);
	
	String onActivate(){
		if (!userExists) return "Index";
		work=null;
		wbList = getAllWorkInProgress();
		return null;
	}
	public List<WorkBean> getAllWorkInProgress(){
		wbList = new ArrayList<WorkBean>();
		for (IBean wb: DAOtundra.getInstance().getAllBeans(new WorkBean()) ){
			if (((WorkBean)wb).isInProgress())
				wbList.add((WorkBean)wb);
		}
		return wbList;
	}
	public String getKindLabelText(){
		String result="";
		if (! wbList.isEmpty())
			for (WorkBean wb : wbList)
				if (work.getId()==wb.getId())
					result="<strong>"+message.get("WorkKinds."+WorkKinds.get(wb.getKind()).getName())+"</strong>";
		return result;
	}
	@OnEvent(component="workNameClick")
	String onNameClicked(long id){
		String result = onEditClicked(id);

		if (result.equals("CreateProject")) result="TheProject";
		else if(result.equals("CreateUStory")) result="TheStory";
		else if(result.equals("CreateTask")) result="TheTask";
		else if(result.equals("CreateSprint")) result="TheSprint";
		
		return result;
	}

	@OnEvent(component="deleteWork")
	String onDeleteClicked(long id){
		if (null!=wbList && wbList.size()>0){
			int i=0;
			for(WorkBean wb: wbList){
				if (wb.getId()==id) {
					DAOtundra.getInstance().delete(wb);
					wbList=getAllWorkInProgress();
					return "WorkList";
				}else i++;
			}
		}
		return null;
	}
	
	@OnEvent(component="editWork")
	String onEditClicked(long id){
		String openPage="WorkList";
		if (wbList.size()>0){
			for(WorkBean wb: wbList)
				if (wb.getId()==id) work=wb;
		}else work=null;
		sprint=null;
		story = null;
		task=null;
		if ( WorkKinds.PROJECT_VALUE == work.getKind())	
			openPage="CreateProject";
		else if(WorkKinds.SPRINT_VALUE == work.getKind()){
			sprint = (SprintBean)work;
			openPage="CreateSprint";
		}else if(WorkKinds.USTORY_VALUE == work.getKind()){
			story = (UStoryBean)work;
			openPage="CreateUStory";
		}else if(WorkKinds.TASK_VALUE == work.getKind()){
			task = (USTaskBean)work;
			openPage="CreateTask";
		}

		return openPage;
	}
	
}
